package com.zhenhao.xsq.ui.activity.login.presenter;

import android.util.Base64;

import com.zhenhao.xsq.data.api.HttpAPIWrapper;
import com.zhenhao.xsq.entity.BaseDataEntity;
import com.zhenhao.xsq.entity.BaseEntity;
import com.zhenhao.xsq.entity.LoginEntity;
import com.zhenhao.xsq.entity.UserInfoEntity;
import com.zhenhao.xsq.entity.login.UserEntity;
import com.zhenhao.xsq.ui.activity.login.LoginActivity;
import com.zhenhao.xsq.ui.activity.login.contract.LoginContract;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author xlei
 * @Package com.example.rxdemo.ui.activity.login
 * @Description: presenter of LoginActivity
 * @date 2019/01/03 14:52:55
 */
public class LoginPresenter implements LoginContract.LoginContractPresenter {

    private final LoginContract.View mView;
    HttpAPIWrapper httpAPIWrapper;
    private CompositeDisposable mCompositeDisposable;
    private LoginActivity mActivity;

    @Inject
    public LoginPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull LoginContract.View
            view, LoginActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }

    @Override
    public void login(Map map) {
        mView.showProgressDialog();
        Disposable subscribe = httpAPIWrapper.userLogin(map).subscribe(new Consumer<BaseDataEntity>() {
            @Override
            public void accept(BaseDataEntity baseEntity) throws Exception {
                mView.closeProgressDialog();
                mView.loginSuccess(baseEntity);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.closeProgressDialog();
                mView.loginFail();
            }
        }, new Action() {
            @Override
            public void run() throws Exception {

            }
        });
        mCompositeDisposable.add(subscribe);
    }

    @Override
    public void getUserInfo() {
        Map<String, String> map = new HashMap<>();
        Disposable disposable = httpAPIWrapper.userInfo(map).subscribe(new Consumer<UserEntity>() {
            @Override
            public void accept(UserEntity baseEntity) throws Exception {
                mView.closeProgressDialog();
                mView.getUserInfoSuccess(baseEntity);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.closeProgressDialog();
            }
        }, new Action() {
            @Override
            public void run() throws Exception {

            }
        });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void getCode(String code, String xmbh) {
        mView.showProgressDialog();
        Map<String, String> map = new HashMap<>();
        Disposable disposable = httpAPIWrapper.getCode(code, xmbh, map).subscribe(new Consumer<BaseEntity>() {
            @Override
            public void accept(BaseEntity baseEntity) throws Exception {
                mView.closeProgressDialog();
                mView.getCodeSuccess(baseEntity);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.closeProgressDialog();
            }
        }, new Action() {
            @Override
            public void run() throws Exception {

            }
        });
        mCompositeDisposable.add(disposable);
    }
}